package com.leetcode.LC;

import java.util.HashMap;

public class LC925 {
    public boolean isLongPressedName(String name, String typed) {

        if (typed.length() < name.length() || (name.length() == typed.length() && !name.equals(typed))) {
            return false;
        }
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < typed.length(); i++) {
            if (map.containsKey(typed.charAt(i))) {
                map.put(typed.charAt(i), map.get(typed.charAt(i)) + 1);
            } else {
                map.put(typed.charAt(i), 1);
            }
        }
        for (int i = 0; i < name.length(); i++) {
            if (map.containsKey(name.charAt(i))) {
                map.put(name.charAt(i), map.get(name.charAt(i)) - 1);
                if (map.get(name.charAt(i)) < 0) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;

        /*
        "alex"
        "aaleexa"
        运行时出错
        */
    }

    //新思路：双指针，分别指向两个字符串
    public boolean isLongPressedName2(String name, String typed) {

        if (typed.length() < name.length() || (name.length() == typed.length() && !name.equals(typed))) {
            return false;
        }

        int a = 0, b = 0;
        while (a < name.length() || b < typed.length()) {

            if (a == name.length()) {
                if (name.charAt(a - 1) == typed.charAt(b)) {
                    b++;
                } else {
                    return false;
                }
            } else if (b == typed.length()) {
                return false;
            } else if (name.charAt(a) == typed.charAt(b)) {
                a++;
                b++;
            } else if (name.charAt(a) != typed.charAt(b)) {
                if (a - 1 < 0) {
                    return false;
                } else if (name.charAt(a - 1) == typed.charAt(b)) {
                    b++;
                } else {
                    return false;
                }
            }

        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new LC925().isLongPressedName2("pyplrz", "zppyypllrzz"));
    }
}
